package com.qumitech.visualnovel.service.oldgame.imp;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.ExcelUtils;
import com.qumitech.visualnovel.component.util.FileUtil;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.game.GameCollectDTO;
import com.qumitech.visualnovel.controller.vo.game.GameCollectReq;
import com.qumitech.visualnovel.controller.vo.game.GameCollectVO;
import com.qumitech.visualnovel.datasource.app.GameServiceMapper;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.game.GameStatisticsDTO;
import com.qumitech.visualnovel.pubdef.dto.game.UserGameAccountRecordDTO;
import com.qumitech.visualnovel.pubdef.pubenum.GameTypeEnum;
import com.qumitech.visualnovel.service.oldgame.OldGameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/5/29 18:34
 */
@Service
public class OldGameServiceImpl implements OldGameService {

    @Autowired
    GameServiceMapper gameServiceMapper;

    private final String GAME_COLLECT_URL = "/game/collect";

    @Override
    public Map<String, Object> getGameCollectVoList(GameCollectReq gameCollectReq) {
        return getgetGameCollectVoList(gameCollectReq);
    }

    @Override
    public Map<String, Object> exportMultipartFile(GameCollectReq gameCollectReq, HttpServletResponse request) throws RuntimeException {
        Map<String, Object> rspMap = getgetGameCollectVoList(gameCollectReq);
        GameCollectDTO gameCollectDTO = JSONObject.parseObject(JSONObject.toJSONString(rspMap), GameCollectDTO.class);
        Map<String, Object> resultMap = new HashMap<>();
        try {

            List<GameCollectVO> amounts = gameCollectDTO.getGameInfo();
            ExcelUtils<GameCollectVO> xls = new ExcelUtils<GameCollectVO>(GameCollectVO.class);
            String filename = FileUtil.getExcelName();
            request.reset();
            request.setContentType("application/vnd.ms-excel;charset=utf-8");
            request.setHeader("Content-Disposition", "attachment;filename=" + filename);
            OutputStream outputStream = request.getOutputStream();
            xls.exportExcel("用户活动信息导出", amounts, outputStream);
            outputStream.flush();
            outputStream.close();
            resultMap.put("title", filename);
            return resultMap;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        throw new BusinessException(ResultEnum.EXCEL_EXPORT_ERROR);


    }

    private Map<String, Object> getgetGameCollectVoList(GameCollectReq gameCollectReq) {
        String url = UrlConstant.SERVER_URL + GAME_COLLECT_URL;
        String req = JSONObject.toJSONString(gameCollectReq);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    public List<GameStatisticsDTO> gameStatisticsList(List<String> uidList, String gameType, Integer startInput, Integer endInput, String startTime, String endTime, Integer offset, Integer limit, String orderBy, String orderType, List<HashMap<String, String>> tableNames) {
        if (orderBy.equals("pound_diamonds")) {
            orderBy = "gaming_diamonds";
        }
        List<GameStatisticsDTO> gameStatistics = gameServiceMapper.gameStatisticsList(uidList, gameType, startInput, endInput, startTime, endTime, offset, limit, orderBy, orderType, tableNames);
        for (GameStatisticsDTO gs: gameStatistics) {
            gs.setGameType(GameTypeEnum.getGameTypeCHN(gs.getGameType()));
        }
        return gameStatistics;
    };

    public int gameStatisticsListCount(List<String> uidList, String gameType, Integer startInput, Integer endInput, String startTime, String endTime, List<HashMap<String, String>> tableNames) {
        return gameServiceMapper.gameStatisticsListCount(uidList, gameType, startInput, endInput, startTime, endTime, tableNames);
    };

    public List<UserGameAccountRecordDTO> listGamerNicknames(Integer accountId, Integer offset, Integer limit, String orderBy, String orderType) {
        return gameServiceMapper.listGamerNicknames(accountId, offset, limit, orderBy, orderType);
    }

    public int countGamerNicknames(Integer accountId) {
        return gameServiceMapper.countGamerNicknames(accountId);
    }


}
